﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ArkanoidX
{
    public class LayerManager
    {
         /**
             * the order of objects in the same layer is not guaranteed
             */
            public List<Layer> _layers = new List<Layer>();
            public Game _game;
            public Dictionary<GameComponent, Layer> _componentToLayer = new Dictionary<GameComponent,Layer>();

            public Game Game { get { return _game; } }

            public LayerManager(Game game)
            {
                _game = game;
            }

            public int GetLayerCount()
            {
                return _layers.Count;
            }

            public Layer GetLayer(int index)
            {
                return _layers[index];
            }

            public Layer CreateLayerAndStack()
            {
                return CreateLayerAndInsertAt(_layers.Count);
            }

            public Layer CreateLayerAndInsertAt(int index)
            {
                Layer layer = new Layer(this, index);
                _layers.Insert(index, layer);
                layer.Manager = this;
                layer.Index = index;
                if (index < _layers.Count - 1)
                {
                    layer.Next = _layers[index + 1];
                    for (int i = index + 1; i < _layers.Count; i++)
                    {
                        _layers[i].Index = i;
                    }
                }
                else
                {
                    layer.Next = null;
                }
                if (index > 0)
                {
                    _layers[index - 1].Next = layer;
                }
                return layer;
            }

            public void RemoveLayer(Layer layer)
            {
                int index = layer.LayerIndex;
                _layers.RemoveAt(index);
                if (index < _layers.Count)
                {
                    for (int i = index; i < _layers.Count; i++)
                    {
                        _layers[i].Index = i;
                    }
                }
                if (index > 0)
                {
                    _layers[index - 1].Next = _layers[index];
                }
            }

            public void RemoveComponent(GameComponent component)
            {
                Layer layer = _componentToLayer[component];
                layer.OneRemoved();
                _componentToLayer.Remove(component);
                _game.Components.Remove(component);
            }

            public void Map(GameComponent component, Layer layer)
            {
                _componentToLayer[component] = layer;
            }

            public void ClearAllComponents()
            {
                foreach (Layer layer in _layers)
                {
                    layer.FirstComponent = 0;
                    layer.OneAfterLastComponent = 0;
                }
                _game.Components.Clear();
            }
    }
}
